package com.ynr.user.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ynr.common.utils.BaseController;
import com.ynr.common.utils.CodeInfoEnum;
import com.ynr.common.utils.CommonResult;
import com.ynr.common.utils.ResultUtil;
import com.ynr.user.model.UserModel;
import com.ynr.user.model.UserModel.LoginGroup;
import com.ynr.user.service.IUserService;

@Controller
public class UserController extends BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);

	@Autowired 
	private IUserService userService;
	
	@RequestMapping(value="/userLogin", method=RequestMethod.POST)
	@ResponseBody
	public CommonResult userLogin(@RequestBody @Validated({LoginGroup.class}) UserModel userModel, BindingResult bindingResult) {
		CommonResult result = validParams(bindingResult);
        if (!result.isSuccess()) {
            return result;
        }
        return userService.login(userModel.getEmail(), userModel.getPassword());
	}
	
	@RequiresPermissions({"user:create"})
	@RequiresRoles({"kk","yu"})
	@RequestMapping(value="/test", method=RequestMethod.GET)
	@ResponseBody
	public CommonResult test() {
        return ResultUtil.returnError(CodeInfoEnum.GLOBAL_TEST);
	}
	
	@RequiresAuthentication
	@RequestMapping("/userLogout")
	@ResponseBody
    public CommonResult userLogout(){  
         SecurityUtils.getSubject().logout();  
         return ResultUtil.returnSuccess(CodeInfoEnum.LOGOUT_SUCCESS);
    }
}